package service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import database.Conn;

public class Customer {

	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblCustomer";
	public boolean AddKhach(String name,String address, 
							int age, int gender, String country,
							int id_card,int id_team,String phone
							){
		connect = Conn.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+
					this.table+"(name,address,age,gender" +
					",nation,id_card,id_team,phone)" +
					" VALUES (?,?,?,?,?,?,?,?)");
			pstm.setString(1,name);
			pstm.setString(2,address);
			pstm.setInt(3,age);
			pstm.setInt(4,gender);
			pstm.setString(5,country);
			pstm.setInt(6,id_card);
			pstm.setInt(7,id_team);
			pstm.setString(8, phone);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			
		}
		return false;
		
	}
	public boolean DeleteCustomer(int id){
		connect = Conn.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean UpdateCustomer(int id, String name,String address, 
			int age, int gender, String country,
			int id_card,int id_team,String phone){
		connect= Conn.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +"SET name=?," +
					"address=?,age=?," +
					"gender=?,nation=?,idcard=?,idteam=?" +
					"phone=?  WHERE id=?");
			pstm.setString(1,name);
			pstm.setString(2,address);
			pstm.setInt(3,age);
			pstm.setInt(4,gender);
			pstm.setString(5,country);
			pstm.setInt(6,id_card);
			pstm.setInt(7,id_team);
			pstm.setString(8, phone);
			pstm.setInt(9,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.Customer> GetAllCustomer(){
		connect= Conn.getConnect();
		ArrayList<entity.Customer> list = new ArrayList<entity.Customer>();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
			while(rs.next()){
				entity.Customer customer = new entity.Customer();
				customer.setId(rs.getInt("id"));
				customer.setName(rs.getString("name"));
				customer.setAddress(rs.getString("address"));
				customer.setCountry(rs.getString("nation"));
				customer.setAge(rs.getInt("age"));
				customer.setGender(rs.getInt("gender"));
				customer.setId_card(rs.getInt("idcard"));
				customer.setId_team(rs.getInt("idteam"));
				customer.setId(rs.getInt("phone"));
				list.add(customer);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}
	public void GetCustomerById(int id){
		
		connect= Conn.getConnect();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
			// Lay thong tin gui den Entity
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	
	
	
}
